ArrayList
An array list is a sequence of objects. Every element of the sequence can be accessed separately. Is modeled like an array, but is much easier than a regular array.
An arrayList is a sequence of objects. The benefit of a ArrayList is that you can keep adding elements to it regardless of the size it was formerly. The size of the ArrayList will automatically increase and no information will be lost. The downfall is that it has no idea what type is stored. To find out the current size of an ArrayList use its size() method. Because only objects can be used in ArrayList, wrapper classes are used instead of primitive data types. Integer, Boolean, Double are the wrapper classes that are used in the place of primitive data types.
+ Benefits 1 It is resizable
2 Not confined by type (ints, doubles, Miis, etc.)
All in one list
- Downfall No idea what type is stored
Comparing Arrays and ArrayLists To make an Array:
int[]name = new int[10];
To make an ArrayList:
ArrayList name = new ArrayList();
To use an Array:
set value name [0] = 3;
To use an ArrayList:
name.set (0, 3);
myMiis.set (5, bob); To add values to an Array:
You can't!
To add values to an ArrayList:
name. add (666);
^ adds to end
To remove a slot from an ArrayList:
nums.remove(4);
Brief intro to ArraysList Methods (Most are self-explanatory)
add ( some object )this adds a slot to the ArrayList (you must add slots before you can set anything) set ( int slot #, some object )after you add a slot, you can assign or set a value or object to the slot (must be done one at a time) get ( int slot # )returns the called value of a slot remove( int slot # )removes the value or object assigned in a slot size ( )returns the size of the ArrayList
Wrapper Classes**
These are classes that we use in the place of primitive data types (instead of int, double, boolean, etc.), because only objects can be used in an ArrayList.
Wrapper Classes: (notice the capital letters!)
Integer
Boolean
Double
Methods:
Integer x = new Integer (16); (This is like saying int x = 16;)
or...
Integer x = 16;
*Because these are objects, we use the proper object methods (like String methods):
x.equals(10); ...returns true or false; we use this in the place of ==
x.compareTo(10); ...returns -1 if x < 10, 0 if x = 10, or 1 if x > 10; you DO NOT use <, >, =, etc.
*To find the value of an Integer (Integer x = 5):
System.out.print(x.intValue()); <---returns a 5
For ArrayLists, when doing something like:
myMiis.get(2).jump();
temporarily store the Mii:
Mii tempMii2 = (Mii) myMiis.get(2);
be sure to cast it (a Mii in this case) otherwise it may not compile.
Your finished section of code should look like this:
Mii tempMii2 = (Mii) myMiis.get(2);
tempMii2.jump();
An array list is a sequence of objects. Every element of the sequence can be accessed separately. Is modeled like an array, but is much easier than a regular array.
An arrayList is a sequence of objects. The benefit of a ArrayList is that you can keep adding elements to it regardless of the size it was formerly. The size of the ArrayList will automatically increase and no information will be lost. The downfall is that it has no idea what type is stored. To find out the current size of an ArrayList use its size() method. Because only objects can be used in ArrayList, wrapper classes are used instead of primitive data types. Integer, Boolean, Double are the wrapper classes that are used in the place of primitive data types.
+ Benefits
1 It is resizable
2 Not confined by type (ints, doubles, Miis, etc.)
All in one list
- Downfall
No idea what type is stored
Comparing Arrays and ArrayLists
To make an Array:
int[]name = new int[10];
To make an ArrayList:
ArrayList name = new ArrayList();
To use an Array:
set value name [0] = 3;
To use an ArrayList:
name.set (0, 3);
myMiis.set (5, bob);
To add values to an Array:
You can't!
To add values to an ArrayList:
name. add (666);
^ adds to end
To remove a slot from an ArrayList:
nums.remove(4);
Brief intro to ArraysList Methods (Most are self-explanatory)
add ( some object ) this adds a slot to the ArrayList (you must add slots before you can set anything)
set ( int slot #, some object ) after you add a slot, you can assign or set a value or object to the slot (must be done one at a time)
get ( int slot # ) returns the called value of a slot
remove( int slot # ) removes the value or object assigned in a slot
size ( ) returns the size of the ArrayList
Wrapper Classes**
These are classes that we use in the place of primitive data types (instead of int, double, boolean, etc.), because only objects can be used in an ArrayList.
Wrapper Classes: (notice the capital letters!)
Methods:
Integer x = new Integer (16); (This is like saying int x = 16;)
or...
Integer x = 16;
*Because these are objects, we use the proper object methods (like String methods):
x.equals(10); ...returns true or false; we use this in the place of ==
x.compareTo(10); ...returns -1 if x < 10, 0 if x = 10, or 1 if x > 10; you DO NOT use <, >, =, etc.
*To find the value of an Integer (Integer x = 5):
System.out.print(x.intValue()); <---returns a 5
For ArrayLists, when doing something like:
temporarily store the Mii:
be sure to cast it (a Mii in this case) otherwise it may not compile.
Your finished section of code should look like this: